package com.yllt4cloud.common.ddbase.util;

import com.yllt4cloud.common.core.constant.CacheConstants;
import com.yllt4cloud.common.ddbase.constant.entity.SessionEntity;
import com.yllt4cloud.common.ddbase.message.MessageQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: yaoxj
 * @create: 2020-01-02 16:01
 **/
@Component
public class CommonRedisUtil {

    @Autowired
    RedisTemplate redisTemplate;

//    redisTemplate.opsForHash().put(VERSION_KEY, hashkey, versionVal);
//    redisTemplate.expire(VERSION_KEY, Constants.EXPIRE, TimeUnit.SECONDS);

    //------------------系统相关redis  BEGIN---------------------------------------
    public void addSystemConfig(String jianMing,String keyValue){
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
        redisTemplate.opsForHash().put(CacheConstants.SYSTEMCONFIG_KEY, jianMing, keyValue);
    }

    public String getSystemConfigValue(String jianMing){
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
       return (String) redisTemplate.opsForHash().get(CacheConstants.SYSTEMCONFIG_KEY, jianMing);
    }
    //------------------系统相关redis  END---------------------------------------



    //------------------用户session相关redis  BEGIN---------------------------------------
    public void addSessionEntity(String sessionId, SessionEntity sessionEntity) {
        redisTemplate.opsForValue().set(getKey(sessionId), sessionEntity, CacheConstants.DD_SESSION_TIMEOUT, TimeUnit.MINUTES);
    }

    public SessionEntity getSessionEntity(String  sessionId) {
        return (SessionEntity) redisTemplate.opsForValue().get(getKey(sessionId));
    }

    //重新设置过期时间
    public boolean reSetExpire(String sessionId){
        return redisTemplate.expire(getKey(sessionId), CacheConstants.DD_SESSION_TIMEOUT, TimeUnit.MINUTES);
    }

    //获取过期时间
    public Long isExpire(String sessionId){
        return  redisTemplate.boundValueOps(getKey(sessionId)).getExpire();
    }

    private String getKey(String sessionId) {
        return CacheConstants.DD_SESSION_CONTAINER+":" + sessionId;
    }

    //------------------用户session相关redis  END---------------------------------------


    //---------------推送相关操作-----------------------
    public void addGroupMember(String groupId, String userId) {
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
        redisTemplate.opsForSet().add(groupId,userId);
    }

    public  Set<String> getGroupMember(String  groupId) {
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
        return redisTemplate.opsForSet().members(groupId);
    }

    public  Long delGroupMember(String  groupId) {
        return redisTemplate.opsForHash().delete(CacheConstants.PUSH_GROUP_MEMBER,groupId);
    }

    public void addUserMsgOffLine(String userId, MessageQueue messageQueue) {
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
        redisTemplate.opsForHash().put(CacheConstants.PUSH_USERMSG_OFFLINE,userId,messageQueue);
    }

    public  MessageQueue getUserMsgOffLine(String  userId) {
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
        return (MessageQueue) redisTemplate.opsForHash().get(CacheConstants.PUSH_USERMSG_OFFLINE,userId);
    }

    public  boolean hasUserMsgOffLine(String  userId) {
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
        return redisTemplate.opsForHash().hasKey(CacheConstants.PUSH_USERMSG_OFFLINE,userId);
    }


}
